package com.example.gmranqi.serviceImpl;

import com.example.gmranqi.common.JsonMapper;
import com.example.gmranqi.dao.BigScreenDao;
import com.example.gmranqi.response.PageResult;
import com.example.gmranqi.service.BigScreenService;
import com.example.gmranqi.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class BigScreenServiceImpl implements BigScreenService {

    private static Logger logger = LoggerFactory.getLogger(BigScreenServiceImpl.class);

    private JsonMapper jm = new JsonMapper(JsonInclude.Include.ALWAYS);

    @Autowired
    private BigScreenDao bigScreenDao;

    @Override
    public PageResult queryDepartTree(int IS_DISTRICT) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            if(IS_DISTRICT==0){//查询职能部门
                dataMap.put("data",bigScreenDao.queryNormDepart());
                pr.setStatus(status);
                pr.setMsg(msg);
                pr.setBody(dataMap);
                return pr;
            }else{//查询行政区划
                List<Map<String, Object>> quList = bigScreenDao.queryDepartByLevel(1);
                if(quList != null && quList.size()>0){
                    for (Map<String, Object> quMap:quList) {
                        List<Map<String, Object>> jdList = bigScreenDao.queryDepartByPid((Long)quMap.get("ID"));
                        if(jdList != null && jdList.size()>0){
                            for (Map<String, Object> jdMap:jdList) {
                                jdMap.put("subList",bigScreenDao.queryDepartByPid((Long)jdMap.get("ID")));
                            }
                        }
                        quMap.put("subList",jdList);
                    }
                }
                dataMap.put("data",quList);
            }

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult countInspectionByDepartIdAndLevel(Long id, Integer levels) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            Map<String,Object> queryMap = new HashMap<>();
            List<Long> departIds = getIdsByDepartIdAndLevel(id, levels);
            queryMap.put("departIds",departIds);
            Date currentTime = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
            String currentYear = sdf1.format(currentTime);
            String currentMonth = sdf2.format(currentTime);
            queryMap.put("CREATE_TIME",currentYear);
            // 当年巡查数量
            dataMap.put("taskCount_year",bigScreenDao.queryTaskCount(queryMap));
            queryMap.put("CREATE_TIME",currentMonth);
            // 本月巡查数量
            dataMap.put("taskCount_month",bigScreenDao.queryTaskCount(queryMap));

            queryMap.remove("CREATE_TIME");
            queryMap.put("start",0);
            queryMap.put("size",10);
            // 待巡查
            queryMap.put("TASK_STATUS",0);
            dataMap.put("taskCount_daixuncha",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("taskList_daixuncha",bigScreenDao.queryTaskList(queryMap));
            // 待整改
            queryMap.put("TASK_STATUS",1);
            dataMap.put("taskCount_daizhenggai",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("taskList_daizhenggai",bigScreenDao.queryTaskList(queryMap));
            // 待复核
            queryMap.put("TASK_STATUS",2);
            dataMap.put("taskCount_daifuhe",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("taskList_daifuhe",bigScreenDao.queryTaskList(queryMap));
            // 已完成
            queryMap.put("TASK_STATUS",3);
            dataMap.put("taskCount_yiwancheng",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("taskList_yiwancheng",bigScreenDao.queryTaskList(queryMap));

//            当年督导数量
            queryMap.remove("start");
            queryMap.remove("size");
            queryMap.remove("TASK_STATUS");
            queryMap.put("DATA_SOURCE",2);
            queryMap.put("CREATE_TIME",currentYear);
            dataMap.put("supervisionCount_year",bigScreenDao.queryTaskCount(queryMap));

//            本月督导数量
            queryMap.put("CREATE_TIME",currentMonth);
            dataMap.put("supervisionCount_month",bigScreenDao.queryTaskCount(queryMap));

//            当年隐患数量
            queryMap.remove("DATA_SOURCE");
            queryMap.put("CREATE_TIME",currentYear);
            queryMap.put("HAS_APPEAL",1);
            dataMap.put("appealCount_year",bigScreenDao.queryTaskCount(queryMap));
//            当年已整改数量
            queryMap.put("FINISH_WAY",2);
            dataMap.put("finishCount_year",bigScreenDao.queryTaskCount(queryMap));

//            当月隐患数量
            queryMap.remove("FINISH_WAY");
            queryMap.put("CREATE_TIME",currentMonth);
            queryMap.put("HAS_APPEAL",1);
            dataMap.put("appealCount_month",bigScreenDao.queryTaskCount(queryMap));
//            当月已整改数量
            queryMap.put("FINISH_WAY",2);
            dataMap.put("finishCount_month",bigScreenDao.queryTaskCount(queryMap));

//            燃气用户隐患数量
            queryMap.remove("CREATE_TIME");
            queryMap.remove("FINISH_WAY");
            queryMap.put("JOB_TYPE_ID",0);
            dataMap.put("userAppealCount",bigScreenDao.queryTaskCount(queryMap));
//            燃气用户隐患已整改数量
            queryMap.put("FINISH_WAY",2);
            dataMap.put("userFinishCount",bigScreenDao.queryTaskCount(queryMap));

//            燃气站点隐患数量
            queryMap.remove("FINISH_WAY");
            queryMap.put("JOB_TYPE_ID",1);
            dataMap.put("siteAppealCount",bigScreenDao.queryTaskCount(queryMap));
//            燃气站点隐患已整改数量
            queryMap.put("FINISH_WAY",2);
            dataMap.put("siteFinishCount",bigScreenDao.queryTaskCount(queryMap));

//            第三放工地隐患数量
            queryMap.remove("FINISH_WAY");
            queryMap.put("JOB_TYPE_ID",2);
            dataMap.put("thirdPartyAppealCount",bigScreenDao.queryTaskCount(queryMap));
//            第三放工地隐患已整改数量
            queryMap.put("FINISH_WAY",2);
            dataMap.put("thirdPartyFinishCount",bigScreenDao.queryTaskCount(queryMap));


//            隐患列表 未完成
            queryMap.remove("JOB_TYPE_ID");
            queryMap.put("start",0);
            queryMap.put("size",10);
            queryMap.put("FINISH_WAY",0);
            dataMap.put("appealCount_weiwancheng",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("appealList_weiwancheng",bigScreenDao.queryTaskList(queryMap));
//            隐患列表 已整改
            queryMap.put("FINISH_WAY",2);
            dataMap.put("appealCount_yizhenggai",bigScreenDao.queryTaskCount(queryMap));
            dataMap.put("appealList_yizhenggai",bigScreenDao.queryTaskList(queryMap));
        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult countMapByStreet() {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
//           燃气站点+燃气管道+燃气用户+第三方工地+隐患
            List<Map<String, Object>> mapList = bigScreenDao.queryDepartByLevel(2);
            for (Map<String, Object> mapItem:mapList) {
                List<Long> departIdList = getIdsByDepartIdAndLevel((Long) mapItem.get("ID"), 2);
                Map<String, Object> mapQuery = new HashMap<>();
                mapQuery.put("departIds",departIdList);
                mapItem.put("gasSiteCount",bigScreenDao.queryGasSiteCount(mapQuery));
                mapItem.put("thirdPartySiteCount",bigScreenDao.queryThirdPartySiteCount(mapQuery));
                mapItem.put("gasUserCount",bigScreenDao.queryGasUserCount(mapQuery));
                mapQuery.put("HAS_APPEAL",1);
                mapItem.put("appealCount",bigScreenDao.queryTaskCount(mapQuery));
                mapQuery.put("START_JD_ID",mapItem.get("ID"));
                mapItem.put("gasPipeCount",bigScreenDao.queryGasPipeLength(mapQuery));
            }
            dataMap.put("data",mapList);
        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult countInspectionByMonth(Map<String,Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String,Object>> dataList = new ArrayList<>();
            Integer ID = (Integer)queryMap.get("ID");
            Integer LEVELS = (Integer)queryMap.get("LEVELS");
            Integer DEPART_ID = (Integer)queryMap.get("DEPART_ID");
            Integer TYPE = (Integer)queryMap.get("TYPE");
            List<Long> departIds = getIdsByDepartIdAndLevel(Long.valueOf(ID), LEVELS);
            queryMap.put("departIds",departIds);
            if(DEPART_ID!=null && DEPART_ID>0){
                queryMap.put("DEPT_ID",DEPART_ID);
            }
            List<String> monthList = new ArrayList<>();
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            for (int i = 0; i < 12; i++) {
                LocalDate monthDate = currentDate.minusMonths(i);
                String monthStr = monthDate.format(formatter);
                monthList.add(monthStr);
            }

            for (String monthStr:monthList) {
                queryMap.put("CREATE_TIME",monthStr);
                Map<String,Object> itemDataMap = new HashMap<>();
                itemDataMap.put("month_str",monthStr);
                if(TYPE==1){//巡检任务
                    itemDataMap.put("record_count",bigScreenDao.queryTaskCount(queryMap));
                }else if(TYPE==2) {//督导
                    queryMap.put("DATA_SOURCE",2);
                    itemDataMap.put("record_count",bigScreenDao.queryTaskCount(queryMap));
                }else if(TYPE==3){//隐患
                    queryMap.put("HAS_APPEAL",1);
                    itemDataMap.put("record_count",bigScreenDao.queryTaskCount(queryMap));
                }
                dataList.add(itemDataMap);
            }
            dataMap.put("dataList",dataList);
        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }




    @Override
    public PageResult countByDepartIdAndLevel(Long id, Integer levels) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("id",id);
            queryMap.put("levels",levels);
            List<Long> departIds = new ArrayList<>();
            departIds.add(id);
//            燃气站点总量
            Integer gasSiteCount = 0;
//            储配站
            Integer gasSiteCount_chuPei = 0;
//            供应站
            Integer gasSiteCount_gongYing = 0;
//            服务站
            Integer gasSiteCount_fuWu = 0;
//            气瓶总量
            Integer cylindersCount = 0;
            // 一级气瓶总量
            Integer cylindersCount_yiji = 0;
            // 二级气瓶总量
            Integer cylindersCount_erji = 0;
            // 三级气瓶总量
            Integer cylindersCount_sanji = 0;

            //第三方工地总量
            Integer thirdPartySiteCount = 0;
            // 已报备 0
            Integer thirdPartySiteCount_baobei = 0;
            // 已建设 1
            Integer thirdPartySiteCount_jianshe = 0;
            // 已完工 2
            Integer thirdPartySiteCount_wangong = 0;

            //燃气管道总量
            Double gasPipeCount = 0.0;
            // 次高压
            Double gasPipeCount_cigaoya = 0.0;
            // 中压
            Double gasPipeCount_zhongya = 0.0;



            List<Map<String,Object>> subList = new ArrayList<>();
            Map<String,Object> gasPipeQueryMap = new HashMap<>();
            if(id == 0){//查询所有
                departIds = null;
                gasPipeCount = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",1);
                gasPipeCount_cigaoya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",2);
                gasPipeCount_zhongya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.remove("TYPE");
                List<Map<String, Object>> quList = bigScreenDao.queryDepartByLevel(1);
                if(quList != null && quList.size()>0){
                    for (Map<String, Object> quMap:quList) {
                        Map<String,Object> subMap = new HashMap<>();
                        Long quId = (Long) quMap.get("ID");
                        subMap.put("id",quId);
                        subMap.put("name",quMap.get("DEPARTMENT_VALUE"));
                        gasPipeQueryMap.put("START_QU_ID",quId);
                        subMap.put("gasPipeCount",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",1);
                        subMap.put("gasPipeCount_cigaoya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",2);
                        subMap.put("gasPipeCount_zhongya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        List<Long> quIds = new ArrayList<>();
                        quIds.add(quId);
                        List<Map<String, Object>> jdList = bigScreenDao.queryDepartByPid(quId);
                        if(jdList != null && jdList.size()>0){
                            for (Map<String, Object> jdMap:jdList) {
                                if((Integer) jdMap.get("IS_DISTRICT") != 1){
                                    continue;
                                }
                                Long jdId = (Long) jdMap.get("ID");
                                quIds.add(jdId);
                                List<Map<String, Object>> sqList = bigScreenDao.queryDepartByPid((Long) jdMap.get("ID"));
                                if(sqList != null && sqList.size()>0){
                                    for (Map<String, Object> sqMap:sqList) {
                                        quIds.add((Long) sqMap.get("ID"));
                                    }
                                }
                            }
                        }
                        Map<String,Object> subQueryMap = new HashMap<>();
                        subQueryMap.put("departIds",quIds);
                        subMap.put("gasSiteCount",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",1);
                        subMap.put("gasSiteCount_chuPei",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",2);
                        subMap.put("gasSiteCount_gongYing",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",3);
                        subMap.put("gasSiteCount_fuWu",bigScreenDao.queryGasSiteCount(subQueryMap));

                        subMap.put("cylindersCount",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",1);
                        subMap.put("cylindersCount_yiji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",2);
                        subMap.put("cylindersCount_erji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",3);
                        subMap.put("cylindersCount_sanji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("build_status",0);
                        subMap.put("thirdPartySiteCount_baobei",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",1);
                        subMap.put("thirdPartySiteCount_jianshe",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",2);
                        subMap.put("thirdPartySiteCount_wangong",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subList.add(subMap);
                    }
                }
            }else if(levels==1){
                gasPipeQueryMap.put("START_QU_ID",id);
                gasPipeCount = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",1);
                gasPipeCount_cigaoya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",2);
                gasPipeCount_zhongya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.remove("TYPE");
                List<Map<String, Object>> jdList = bigScreenDao.queryDepartByPid(id);
                if(jdList != null && jdList.size()>0){
                    for (Map<String, Object> jdMap:jdList) {
                        if((Integer) jdMap.get("IS_DISTRICT") != 1){
                            jdList.remove(jdMap);
                            continue;
                        }
                        Map<String,Object> subMap = new HashMap<>();
                        Long jdId = (Long) jdMap.get("ID");
                        subMap.put("id",jdId);
                        subMap.put("name",jdMap.get("DEPARTMENT_VALUE"));
                        gasPipeQueryMap.put("START_JD_ID",jdId);
                        subMap.put("gasPipeCount",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",1);
                        subMap.put("gasPipeCount_cigaoya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",2);
                        subMap.put("gasPipeCount_zhongya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));

                        List<Long> jdIds = new ArrayList<>();
                        jdIds.add(jdId);
                        departIds.add(jdId);
                        List<Map<String, Object>> sqList = bigScreenDao.queryDepartByPid((Long) jdMap.get("ID"));
                        if(sqList != null && sqList.size()>0){
                            for (Map<String, Object> sqMap:sqList) {
                                departIds.add((Long) sqMap.get("ID"));
                                jdIds.add((Long) sqMap.get("ID"));
                            }
                        }
                        Map<String,Object> subQueryMap = new HashMap<>();
                        subQueryMap.put("departIds",jdIds);
                        subMap.put("gasSiteCount",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",1);
                        subMap.put("gasSiteCount_chuPei",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",2);
                        subMap.put("gasSiteCount_gongYing",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",3);
                        subMap.put("gasSiteCount_fuWu",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subMap.put("cylindersCount",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",1);
                        subMap.put("cylindersCount_yiji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",2);
                        subMap.put("cylindersCount_erji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",3);
                        subMap.put("cylindersCount_sanji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("build_status",0);
                        subMap.put("thirdPartySiteCount_baobei",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",1);
                        subMap.put("thirdPartySiteCount_jianshe",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",2);
                        subMap.put("thirdPartySiteCount_wangong",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subList.add(subMap);
                    }
                }
                queryMap.put("departIds",departIds);
            }else if(levels==2){
                gasPipeQueryMap.put("START_JD_ID",id);
                gasPipeCount = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",1);
                gasPipeCount_cigaoya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.put("TYPE",2);
                gasPipeCount_zhongya = bigScreenDao.queryGasPipeLength(gasPipeQueryMap);
                gasPipeQueryMap.remove("TYPE");

                List<Map<String, Object>> sqList = bigScreenDao.queryDepartByPid(id);
                if(sqList != null && sqList.size()>0){
                    for (Map<String, Object> sqMap:sqList) {
                        Map<String,Object> subMap = new HashMap<>();
                        Long sqId = (Long) sqMap.get("ID");
                        subMap.put("id",sqId);
                        subMap.put("name",sqMap.get("DEPARTMENT_VALUE"));
                        gasPipeQueryMap.put("START_SQ_ID",sqId);
                        subMap.put("gasPipeCount",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",1);
                        subMap.put("gasPipeCount_cigaoya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        gasPipeQueryMap.put("TYPE",2);
                        subMap.put("gasPipeCount_zhongya",bigScreenDao.queryGasPipeLength(gasPipeQueryMap));
                        List<Long> sqIds = new ArrayList<>();
                        sqIds.add(sqId);
                        Map<String,Object> subQueryMap = new HashMap<>();
                        subQueryMap.put("departIds",sqIds);
                        subMap.put("gasSiteCount",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",1);
                        subMap.put("gasSiteCount_chuPei",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",2);
                        subMap.put("gasSiteCount_gongYing",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subQueryMap.put("siteType",3);
                        subMap.put("gasSiteCount_fuWu",bigScreenDao.queryGasSiteCount(subQueryMap));
                        subMap.put("cylindersCount",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",1);
                        subMap.put("cylindersCount_yiji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",2);
                        subMap.put("cylindersCount_erji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("siteLevel",3);
                        subMap.put("cylindersCount_sanji",bigScreenDao.queryCylindersCount(subQueryMap));
                        subQueryMap.put("build_status",0);
                        subMap.put("thirdPartySiteCount_baobei",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",1);
                        subMap.put("thirdPartySiteCount_jianshe",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subQueryMap.put("build_status",2);
                        subMap.put("thirdPartySiteCount_wangong",bigScreenDao.queryThirdPartySiteCount(subQueryMap));
                        subList.add(subMap);
                        departIds.add((Long) sqMap.get("ID"));
                    }
                }
                queryMap.put("departIds",departIds);
            }else if(levels==3){
                queryMap.put("departIds",departIds);
            }
            gasSiteCount = bigScreenDao.queryGasSiteCount(queryMap);
            cylindersCount = bigScreenDao.queryCylindersCount(queryMap);
            queryMap.put("siteType",1);
            gasSiteCount_chuPei = bigScreenDao.queryGasSiteCount(queryMap);

            queryMap.put("siteType",2);
            gasSiteCount_gongYing = bigScreenDao.queryGasSiteCount(queryMap);

            queryMap.put("siteType",3);
            gasSiteCount_fuWu = bigScreenDao.queryGasSiteCount(queryMap);

            queryMap.put("siteType",null);
            queryMap.put("siteLevel",1);
            cylindersCount_yiji= bigScreenDao.queryCylindersCount(queryMap);
            queryMap.put("siteLevel",2);
            cylindersCount_erji = bigScreenDao.queryCylindersCount(queryMap);
            queryMap.put("siteLevel",3);
            cylindersCount_sanji = bigScreenDao.queryCylindersCount(queryMap);

            thirdPartySiteCount = bigScreenDao.queryThirdPartySiteCount(queryMap);
            queryMap.put("build_status",0);
            thirdPartySiteCount_baobei = bigScreenDao.queryThirdPartySiteCount(queryMap);
            queryMap.put("build_status",1);
            thirdPartySiteCount_jianshe = bigScreenDao.queryThirdPartySiteCount(queryMap);
            queryMap.put("build_status",2);
            thirdPartySiteCount_wangong =bigScreenDao.queryThirdPartySiteCount(queryMap);

//            燃气站点数据
            dataMap.put("gasSiteCount",gasSiteCount);
            dataMap.put("gasSiteCount_chuPei",gasSiteCount_chuPei);
            dataMap.put("gasSiteCount_gongYing",gasSiteCount_gongYing);
            dataMap.put("gasSiteCount_fuWu",gasSiteCount_fuWu);
            dataMap.put("subList",subList);

//            瓶装气存量统计
            dataMap.put("cylindersCount",cylindersCount);
            dataMap.put("cylindersCount_yiji",cylindersCount_yiji);
            dataMap.put("cylindersCount_erji",cylindersCount_erji);
            dataMap.put("cylindersCount_sanji",cylindersCount_sanji);
//          第三方工地数据
            dataMap.put("thirdPartySiteCount",thirdPartySiteCount);
            dataMap.put("thirdPartySiteCount_baobei",thirdPartySiteCount_baobei);
            dataMap.put("thirdPartySiteCount_jianshe",thirdPartySiteCount_jianshe);
            dataMap.put("thirdPartySiteCount_wangong",thirdPartySiteCount_wangong);
//          燃气管道
            dataMap.put("gasPipeCount",gasPipeCount);
            dataMap.put("gasPipeCount_cigaoya",gasPipeCount_cigaoya);
            dataMap.put("gasPipeCount_zhongya",gasPipeCount_zhongya);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    private List<Long> getIdsByDepartIdAndLevel(Long id, Integer levels){
        List<Long> departIds = new ArrayList<>();
        if(id == 0){
            departIds = bigScreenDao.getAllIds();
        }else if(levels == 1){
            departIds = bigScreenDao.getIdsByLevelOne(id);
        }else if(levels == 2){
            departIds = bigScreenDao.getIdsByLevelTwo(id);
        }else if(levels == 3){
            departIds.add(id);
        }
        return departIds;
    }

    @Override
    public PageResult query_G_GAS_SITE(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryGasSiteCount(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryList_G_GAS_SITE(queryMap);
                /*for (Map<String, Object> itemMap:dataList) {
                    BigDecimal ID = (BigDecimal)itemMap.get("ID");
                    itemMap.put("ID",ID.toString());
                    Map<String, Object> taskMap = bigScreenDao.queryTaskByINSPECTOR_ID_B(ID);
                    if(taskMap != null && taskMap.get("HAS_APPEAL") != null){
                        Integer HAS_APPEAL = (Integer)taskMap.get("HAS_APPEAL");
                        itemMap.put("HAS_APPEAL",HAS_APPEAL);
                    }else{
                        itemMap.put("HAS_APPEAL",0);
                    }
                }*/
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult query_G_GAS_USER(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryGasUserCount(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryList_G_GAS_USER(queryMap);
                /*for (Map<String, Object> itemMap:dataList) {
                    Map<String, Object> taskMap = bigScreenDao.queryTaskByINSPECTOR_ID_L((Long)itemMap.get("ID"));
                    if(taskMap != null && taskMap.get("HAS_APPEAL") != null){
                        Integer HAS_APPEAL = (Integer)taskMap.get("HAS_APPEAL");
                        itemMap.put("HAS_APPEAL",HAS_APPEAL);
                    }else{
                        itemMap.put("HAS_APPEAL",0);
                    }
                }*/
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult query_G_THIRD_PARTY_SITE(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryThirdPartySiteCount(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryList_G_THIRD_PARTY_SITE(queryMap);
                /*for (Map<String, Object> itemMap:dataList) {
                    BigDecimal ID = (BigDecimal)itemMap.get("ID");
                    itemMap.put("ID",ID.toString());
                    Map<String, Object> taskMap = bigScreenDao.queryTaskByINSPECTOR_ID_B(ID);
                    if(taskMap != null && taskMap.get("HAS_APPEAL") != null){
                        Integer HAS_APPEAL = (Integer)taskMap.get("HAS_APPEAL");
                        itemMap.put("HAS_APPEAL",HAS_APPEAL);
                    }else{
                        itemMap.put("HAS_APPEAL",0);
                    }
                }*/
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult query_G_RQAQ_TASK(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryTaskCount(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryTaskList(queryMap);
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult queryTaskWithPoints(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryTaskWithPointsCount(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryTaskWithPointsList(queryMap);
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }


    @Override
    public PageResult query_G_GAS_PIPE(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = new ArrayList<>();
            Integer count = bigScreenDao.queryCount_G_GAS_PIPE(queryMap);
            if(count>0){
                dataList = bigScreenDao.queryList_G_GAS_PIPE(queryMap);
            }
            dataMap.put("count",count);
            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult queryTaskDetailCount(Map<String, Object> queryMap) {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
//            第三方工地 燃气站点    燃气用户    隐患点（统计整个楼栋的）
//            统计 本月巡检次数  本月督导次数  隐患数量  已整改数量
//            taskCount_month supervisionCount_month appealCount finishCount
            int type = (int)queryMap.get("type");
            String idStr = (String)queryMap.get("id");
            Long id = Long.valueOf(idStr);
            queryMap.remove("type");
            queryMap.remove("id");

            Date currentTime = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String currentMonth = sdf.format(currentTime);
            int taskCount_month = 0;
            int supervisionCount_month = 0;
            int appealCount = 0;
            int finishCount = 0;
            if(type==1){//第三方工地
                dataMap = bigScreenDao.queryThirdPartySiteById(id);
            }else if(type==2){//燃气站点
                dataMap = bigScreenDao.queryGasSiteById(id);
            }else if(type==3){//燃气用户
                dataMap = bigScreenDao.queryGasUserById(id);
                //                添加楼栋用户数量
                String HOUSE_CODE = (String)dataMap.get("HOUSE_CODE");
                if(StringUtils.isNotEmpty(HOUSE_CODE)){
                    queryMap.put("HOUSE_CODE",HOUSE_CODE);
                    queryMap.put("BUILD_CODE",HOUSE_CODE);
                    dataMap.put("buildUserCount",bigScreenDao.queryGasUserCount(queryMap));
                }else {
                    dataMap.put("buildUserCount",0);
                }
            }else if(type==4){//隐患点（统计整个楼栋的）
                dataMap = bigScreenDao.queryTaskById(id);
                //                添加楼栋用户数量
                String BUILD_CODE = (String)dataMap.get("BUILD_CODE");
                if(StringUtils.isNotEmpty(BUILD_CODE)){
                    queryMap.put("BUILD_CODE",BUILD_CODE);
                    queryMap.put("HOUSE_CODE",BUILD_CODE);
                    dataMap.put("buildUserCount",bigScreenDao.queryGasUserCount(queryMap));
                }else {
                    dataMap.put("buildUserCount",0);
                }
            }
            queryMap.put("CREATE_TIME",currentMonth);
            if(queryMap.get("BUILD_CODE")==null){
                queryMap.put("INSPECTOR_ID",id);
            }
            taskCount_month= bigScreenDao.queryTaskCount(queryMap);
            queryMap.put("DATA_SOURCE",2);
            supervisionCount_month= bigScreenDao.queryTaskCount(queryMap);
            queryMap.remove("CREATE_TIME");
            queryMap.remove("DATA_SOURCE");
            queryMap.put("HAS_APPEAL",1);
            appealCount= bigScreenDao.queryTaskCount(queryMap);
            queryMap.put("FINISH_WAY",2);
            finishCount= bigScreenDao.queryTaskCount(queryMap);

            dataMap.put("taskCount_month",taskCount_month);
            dataMap.put("supervisionCount_month",supervisionCount_month);
            dataMap.put("appealCount",appealCount);
            dataMap.put("finishCount",finishCount);
        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }

    @Override
    public PageResult query_SYS_DEPARTMENT() {
        return null;
    }

    @Override
    public PageResult query_G_ADMINISTRATION() {
        PageResult pr = new PageResult();
        int status = 1;
        String msg = "查询成功！";
        Map<String,Object> dataMap = new HashMap<>();
        try{
            List<Map<String, Object>> dataList = bigScreenDao.query_G_ADMINISTRATION();

            dataMap.put("data",dataList);

        }catch (Exception e){
            status = 2;
            msg = "查询异常： " + e;
            logger.info("查询异常：" , e);
        }
        pr.setStatus(status);
        pr.setMsg(msg);
        pr.setBody(dataMap);
        return pr;
    }




}
